/**
 * 从m中取n的所有组合
 * @param m
 * @param n
 * @return {*[]}
 */
// TODO:待优化
export const getFlagArrs = (m, n) => {
  let flagArrs = [],
    flagArr = [],
    isEnd = false;
  for (let i = 0; i < m; i++) {
    flagArr[i] = i < n ? 1 : 0;
  }
  flagArrs.push(flagArr.concat());
  // 当n不等于0并且m大于n的时候进入
  if (n && m > n) {
    while (!isEnd) {
      let leftCnt = 0;
      for (let i = 0; i < m - 1; i++) {
        if (flagArr[i] === 1 && flagArr[i + 1] === 0) {
          for (let j = 0; j < i; j++) {
            flagArr[j] = j < leftCnt ? 1 : 0;
          }
          flagArr[i] = 0;
          flagArr[i + 1] = 1;
          let aTmp = flagArr.concat();
          flagArrs.push(aTmp);
          if (aTmp.slice(-n).join("").indexOf("0") === -1) {
            isEnd = true;
          }
          break;
        }
        flagArr[i] === 1 && leftCnt++;
      }
    }
  }
  return flagArrs;
};
export const getArrayCombine = targetArr => {
  if (!Array.isArray(targetArr)) {
    return new Error("传入的参数必须是数组");
  }
  let resultArr = [];
  for (let n = 0; n <= targetArr.length; n++) {
    let flagArrs = getFlagArrs(targetArr.length, n);
    while (flagArrs.length) {
      let flagArr = flagArrs.shift();
      let combArr = Array(targetArr.length);
      for (let i = 0; i < targetArr.length; i++) {
        if (flagArr[i]) {
          combArr[i] = targetArr[i];
        }
      }
      resultArr.push(combArr);
    }
  }
  return resultArr;
};
